From 5fc17b25b2c03345cc01b7ad06575c1c2b541ca7 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Fri, 31 Jan 2020 10:56:02 -0700 Subject: [PATCH] Fix memory leak in lowranceusr version 4 writer. (#487) * fix memory leak in lowranceusr version 4 writer. memory was still reachable, due to global pointer it wasn't lost. * fix include. --- lowranceusr.cc | 86 ++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 48 deletions(-) diff --git a/lowranceusr.cc b/lowranceusr.cc index 1f006e452..740e9a6b0 100644 --- a/lowranceusr.cc +++ b/lowranceusr.cc @@ -85,28 +85,32 @@ */ -#include // for M_PI, atan, exp, log, tan -#include // for printf, snprintf, sprintf, SEEK_CUR -#include // for atoi, abs -#include // for strlen, strcmp -#include - -#include // for QByteArray -#include // for QDate -#include // for QDateTime -#include // for QLatin1String -#include // for QScopedPointer -#include // for QString, operator+, operator==, operator!= -#include // for QTextCodec -#include // for QTextEncoder -#include // for QTime -#include // for CaseInsensitive, UTC -#include // for qPrintable, uint, foreach -#include +#include // for min +#include // for M_PI, round, atan, exp, log, tan +#include // for printf, sprintf, SEEK_CUR +#include // for atoi, abs +#include // for strcmp, strlen +#include // for time_t + +#include // for QByteArray +#include // for QDate +#include // for QDateTime +#include // for QLatin1String +#include // for QList +#include // for QScopedPointer +#include // for QString, operator+, operator==, operator!= +#include // for QStringRef +#include // for QTextCodec, QTextCodec::IgnoreHeader +#include // for QTextEncoder +#include // for QTime +#include // for QVector +#include // for CaseInsensitive, UTC +#include // for qPrintable, uint, qAsConst, QAddConst<>::Type #include "defs.h" -#include "gbfile.h" // for gbfgetint32, gbfputint32, gbfputint16, gbfgetc, gbfgetint16, gbfputc, gbfwrite, gbfeof, gbfgetflt, gbfclose, gbfgetdbl, gbfputdbl, gbfile, gbfputflt, gbfread, gbfseek, gbfopen_le -#include "src/core/datetime.h" // for DateTime +#include "gbfile.h" // for gbfgetint32, gbfputint32, gbfputint16, gbfgetc, gbfgetint16, gbfwrite, gbfputc, gbfeof, gbfgetflt, gbfclose, gbfgetdbl, gbfopen_le, gbfputdbl, gbfputs, gbfile, gbfputflt, gbfread, gbfseek +#include "src/core/datetime.h" // for DateTime +#include "src/core/logging.h" // for Warning struct lowranceusr_icon_mapping_t { @@ -337,9 +341,7 @@ static char* opt_content_descr; static char* opt_serialnum; static int opt_serialnum_i; -static const Waypoint** waypt_table; -static int waypt_table_sz; -static int waypt_table_ct; +static QList* waypt_table{nullptr}; /* from waypt.c, we need to iterate over waypoints when extracting routes */ extern WaypointList* global_waypoint_list; @@ -442,30 +444,18 @@ same_points(const Waypoint* A, const Waypoint* B) static void register_waypt(const Waypoint* wpt) { - for (int i = 0; i < waypt_table_ct; i++) { - const Waypoint* cmp = waypt_table[i]; - + for (const Waypoint* cmp : qAsConst(*waypt_table)) { if (same_points(wpt, cmp)) { return; } } - if (waypt_table_ct >= waypt_table_sz) { - waypt_table_sz += 32; - if (waypt_table) { - waypt_table = (const Waypoint**) xrealloc(waypt_table, waypt_table_sz * sizeof(wpt)); - } else { - waypt_table = (const Waypoint**) xmalloc(waypt_table_sz * sizeof(wpt)); - } - } - if (global_opts.debug_level >= 2) { printf(MYNAME " adding waypt %s (%s) to table at index %d\n", - qPrintable(wpt->shortname), qPrintable(wpt->description), waypt_table_ct); + qPrintable(wpt->shortname), qPrintable(wpt->description), waypt_table->size()); } - waypt_table[waypt_table_ct] = wpt; - waypt_table_ct++; + waypt_table->append(wpt); } /* end borrowed from raymarine.c */ @@ -757,6 +747,7 @@ wr_init(const QString& fname) fatal(MYNAME " wversion value %s is not supported !!\n", opt_wversion); } utf16le_codec = QTextCodec::codecForName("UTF-16LE"); + waypt_table = new QList; } static void @@ -765,6 +756,8 @@ wr_deinit() gbfclose(file_out); mkshort_del_handle(&mkshort_handle); utf16le_codec = nullptr; + delete waypt_table; + waypt_table = nullptr; } /** @@ -1894,24 +1887,21 @@ lowranceusr4_write_waypoints() { /* enumerate all waypoints from both the plain old waypoint list and also all routes */ - waypt_table_sz = 0; - waypt_table_ct = 0; - waypt_table = nullptr; waypt_disp_all(register_waypt); route_disp_all(nullptr, nullptr, register_waypt); if (global_opts.debug_level >= 1) { - printf(MYNAME " writing %d waypoints\n", waypt_table_ct); + printf(MYNAME " writing %d waypoints\n", waypt_table->size()); } - gbfputint32(waypt_table_ct, file_out); + gbfputint32(waypt_table->size(), file_out); waypt_uid = 0; - for (int i = 0; i < waypt_table_ct; ++i) { + for (int i = 0; i < waypt_table->size(); ++i) { if (global_opts.debug_level >= 2) { printf(MYNAME " writing out waypt %d (%s - %s)\n", - i, qPrintable(waypt_table[i]->shortname), qPrintable(waypt_table[i]->description)); + i, qPrintable(waypt_table->at(i)->shortname), qPrintable(waypt_table->at(i)->description)); } - lowranceusr4_waypt_disp((static_cast(waypt_table[i]))); + lowranceusr4_waypt_disp((waypt_table->at(i))); } } @@ -2068,8 +2058,8 @@ lowranceusr4_route_hdr(const route_head* rte) static void lowranceusr4_route_leg_disp(const Waypoint* wpt) { - for (int i = 0; i < waypt_table_ct; i++) { - const Waypoint* cmp = waypt_table[i]; + for (int i = 0; i < waypt_table->size(); i++) { + const Waypoint* cmp = waypt_table->at(i); if (cmp->shortname == wpt->shortname) { auto* fs = (lowranceusr4_fsdata*) fs_chain_find(cmp->fs, FS_LOWRANCEUSR4); -- 2.30.2